package org.hank.demoproject

import android.util.Log
import org.hank.demoproject.utils.DES3
import org.hank.demoproject.utils.HexUtil
import org.hank.demoproject.utils.ISOUtils
import org.junit.Test
import java.math.BigDecimal
import java.text.DecimalFormat
import java.text.NumberFormat
import java.text.SimpleDateFormat
import java.util.*

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 *@auth: Hank
 *邮箱: cs16xiaoc1@163.com
 *创建时间: 2021/5/15 10:34
 *描述:
 */
class Test {
    var lambda1: () -> Unit = { System.out.println("lambda1") }
    var lambda2: (str: String) -> Unit = { it -> System.out.println(it) }
    var lambda3: (str: String) -> Boolean = { it ->
        System.out.println(it)
        false
    }
    var lambda4: String.(str: String) -> Unit = {
        System.out.println(this)
        System.out.println(it)
    }
    var lambda5: () -> TestLambda = {
        System.out.println("create lambda5")
        TestLambda()
    }
    var lambda6: (lambda6: () -> String) -> Unit = {}

    @Test
    fun lambdaTest() {
        lambda1.invoke()
        lambda2.invoke("lambda2")
        System.out.println(lambda3.invoke("lambda3"))
        "abc".lambda4("efg")
        lambda5.invoke()()
        lambda6(::funLambda)
    }

    @Test
    fun test() {
        var objectLong: Any? = null
        objectLong = 100
        var longValue: Long = objectLong.toString().toLong()
        System.out.println(longValue)
    }

    @Test
    fun str2AsciiSpdh() {
        var fullMessage =
            "9.63BPE000001             210807085511FO001500001CB661CD01CP11CS0000521Cb511FAF62B94AEE381Cd111         1Ce001Ch00102408811Cq;4176662220010042=011220110816243?1C61EE0721EI9781EN15051EO0140196210807C815C5EFCB22C53120000002CDF23FA7000000000000978000000000066060111039000000A019999999999374A87EE1EP010922000000008CA00000000310101Eq019F660436004000"
        var realLength =
            fullMessage.length - ((fullMessage.split("1C").size - 1) + (fullMessage.split("1E").size - 1)) * 3
        var header = ISOUtils.padleft(realLength.toString(16), 4, '0')
        var splitStr = fullMessage.split("1C")
        var strBuilder = StringBuilder()
        for (item in splitStr) {
            if (item.indexOf("1E") == -1) {
                strBuilder.append(HexUtil.bytes2HexString(item.toByteArray()))
            } else {
                var splitSubfield = item.split("1E")
                for (item1 in splitSubfield) {
                    strBuilder.append(HexUtil.bytes2HexString(item1.toByteArray()))
                    if (splitSubfield.indexOf(item1) != splitSubfield.size - 1)
                        strBuilder.append("1E")
                }
            }
            if (splitStr.indexOf(item) != splitStr.size - 1)
                strBuilder.append("1C")
        }
        var result = header + strBuilder.toString()
        System.out.println(result)
    }

    fun funLambda(): String {
        System.out.println("funLambda")
        return "funLambda"
    }

    @Test
    fun swapAB() {
        var a = 3
        var b = 7
        //使用异或交换
        a = a xor b
        b = a xor b
        a = a xor b
        //使用加法交换
//        a = a + b
//        b = a - b
//        a = a - b
        System.out.println("a:${a} b:${b}")
    }

    @Test
    fun printSysdate() {
        var date = SimpleDateFormat("MMDD").format(Date())
        System.out.println(date)
    }


    @Test
    fun main() {
//        val clearText = "0"
//        val key = "12345678" //密钥
//        println("明文：$clearText\n密钥：$key")
//        val encryptText = DesECBUtil.encryptDES(clearText, key)
//        println("加密后：$encryptText")
//        val decryptText = DesECBUtil.decryptDES(encryptText, key)
//        println("解密后：$decryptText")
        val content = "测试3DES"
        val key = "key"
        println("content:$content")
        val s1 = DES3.encrypt(content, key)
        println("加密:$s1")
        println("解密:" + DES3.decrypt(s1, key))
    }

    @Test
    fun arrayCopy() {
        var a = byteArrayOf(0x09, 0x06, 0x0A)
        var b = byteArrayOf(0x00, 0x00, 0x00)
        System.arraycopy(a, 0, b, 0, 3)
        System.out.println("a = ${ISOUtils.byte2hex(a)} b = ${ISOUtils.byte2hex(b)}")
    }

    @Test
    fun reverse() {
        var a = "182326212-228325495"
        var strBuffer = StringBuffer()
        strBuffer.append(a)
        System.out.println(strBuffer.reverse().toString())
    }

    @Test
    fun testAnd() {
        val packName = "PDD00001"
        val KeyType: String = packName.substring(0, 2)
        val keyPurpose: String = packName.substring(2, 3)
        val keyIndex: String = packName.substring(4, 8)
        System.out.println("$KeyType == $keyPurpose == $keyIndex")
    }

    @Test
    fun myTest(){
        System.out.println(("0.5".toBigDecimal().divide(BigDecimal(10))).setScale(2))
    }
    @Test
    fun strFormate(){
            var format: NumberFormat = DecimalFormat("#,##0.00")
        var a = "1"
        a.toBigDecimal()
            System.out.println(format.format(BigDecimal(3000000)))
    }
}

class User {
    var name = "张三"
    var age = 18
}

class TestLambda : () -> Unit {
    override fun invoke() {
        System.out.println("testlambda")
    }
}

fun String?.Contain1(): Boolean {
    if (this != null)
        return contains("1")
    else
        return false
}
